package com.faiz.algorithm;

import java.util.*;

public class DailyTemperatures {
    public int[] dailyTemperatures(int[] temperatures) {
        Deque<int[]> stack = new LinkedList<>();
        int[] res = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
            int[] curElement = {i, temperatures[i]};

            while (!stack.isEmpty() && curElement[1] > stack.peek()[1]) {
                int[] element = stack.pop();
                res[element[0]] = curElement[0] - element[0];

            }

            stack.push(curElement);
        }
        return res;
    }

    public static void main(String[] args) {
        int[] temperatures = {73, 74, 75, 71, 69, 72, 76, 73};
        DailyTemperatures dailyTemperatures = new DailyTemperatures();
        System.out.println(Arrays.toString(dailyTemperatures.dailyTemperatures(temperatures)));

    }
}
